package com.lt.base;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.FloatRange;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.AppCompatDialog;

import com.lt.action.HandlerAction;
import com.lt.common.DebugHelper;

/**
 * Created by tianlan on 2017/9/27.
 */

public class BaseDialog extends AppCompatDialog implements HandlerAction {
    protected final String TAG = this.getClass().getSimpleName();

    protected Activity mActivity;

    protected BaseDialog(Context context) {
        super(context);
    }

    protected BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    protected BaseDialog(Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    protected boolean showDialog() {
        if (mActivity.isFinishing()) {
            return false;
        }
        if (isShowing()) {
            dismiss();
        }
        try {
            super.show();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean isShow() {
        return mActivity != null && !mActivity.isFinishing() && isShowing();
    }

    /**
     * 设置宽高
     */
    protected void setDialogSize(int width, int height) {
        Window window = this.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            if (width != 0) {
                lp.width = width;
            }
            if (height != 0) {
                lp.height = height;
            }
            window.setAttributes(lp);
        }
    }


    /**
     * 设置 Dialog 重心
     */
    protected void setGravity(int gravity) {
        Window window = getWindow();
        if (window != null) {
            window.setGravity(gravity);
        }
    }

    /**
     * 设置背景遮盖层开关
     */
    public void setBackgroundDimEnabled(boolean enabled) {
        Window window = getWindow();
        if (window != null) {
            if (enabled) {
                window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            } else {
                window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            }
        }
    }

    /**
     * 设置背景遮盖层的透明度（前提条件是背景遮盖层开关必须是为开启状态）
     */
    public void setBackgroundDimAmount(@FloatRange(from = 0, to = 1) float dimAmount) {
        Window window = getWindow();
        if (window != null) {
            window.setDimAmount(dimAmount);
        }
    }

    protected void log(String str) {
        DebugHelper.e(TAG, str);
    }

    public void close() {
        if (isShow()) {
            try {
                dismiss();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (RuntimeException e) {
                e.printStackTrace();
            }

        }
    }

    //解决输入无法弹出的问题
    protected void setSoftInputEnable() {
        if (getWindow() != null) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        }
    }


    /**
     * 延迟一段时间执行
     */
//    public final void postDelayed(Runnable r, long delayMillis) {
//        if (isShowing()) {
//            postDelayed(r, delayMillis);
//        } else {
//            addOnShowListener(new ShowPostDelayedWrapper(r, delayMillis));
//        }
//    }

}
